package office.service.sale.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import current.security.user.domain.User;

import office.action.sale.form.CustomerCareConditionForm;
import office.action.sale.form.CustomerCareForm;
import office.action.sale.wrapper.CustomerCareWrapper;
import office.action.workflow.form.RemindForm;
import office.bo.sale.impl.Customer;
import office.bo.sale.impl.CustomerCare;
import office.domainset.BizDomainProviderSetImpl;
import office.service.sale.ICustomerCareService;
import office.service.workflow.IRemindService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class CustomerCareServiceImpl extends BaseBizServiceImpl implements ICustomerCareService{
	private  IRemindService remindService;
	public void editCustomerCare(CustomerCareForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			CustomerCare bo = (CustomerCare)domainProviderSetImpl.getCustomerCareDP().newEmptyObj();
			bo =(CustomerCare) form.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑客户关怀失败!", e);
		}
	}
	
	public Page getPageInfo(CustomerCareConditionForm condition)throws BusinessException{
		Page page = new Page();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getCustomerCareDP();
			int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "CustomerCare.pageSize"));
			page.setPageSize(pageSize);
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getTitle())){
				condMap.put("title", condition.getTitle());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider, "findCustomerCareByIdAndName", condMap,new HashMap());
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
			
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	
	public List<CustomerCareWrapper> findAllCustomerCare(CustomerCareConditionForm condition,Page page)throws BusinessException{
		List<CustomerCareWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getCustomerCareDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getTitle())){
				condMap.put("title", condition.getTitle());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findCustomerCareByIdAndName",condMap,new HashMap(),page);
			CustomerCareWrapper wrapper;
			for(int i=0;i<list.size();i++){
				CustomerCare bo = (CustomerCare)list.get(i);
				Customer customer =bo.getCustomer(domainProviderSet);
				User user =bo.getUser(domainProviderSet);
				bo.setCustomerName(customer.getName());
				bo.setPrincipalName(user.getCname()); 
				wrapper = new CustomerCareWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取客户关怀列表失败！", e);
		}
		return wrapperList;
	}

	public void delCustomerCareById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getCustomerCareDP();
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除客户关怀失败！", e);
		}
	}
	
	public CustomerCareForm getById(String id)throws BusinessException{
		CustomerCareForm form = new CustomerCareForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			CustomerCare bo = (CustomerCare)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getCustomerCareDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取客户关怀失败!", e);
		}
		return form;
	}

	public void sendNote() throws BusinessException {
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			StringBuffer  buffer=new StringBuffer();
			buffer.append("select  t.* from  SALE_CustomerCare t ,");
			buffer.append(" ( select   t.principal  as principal, max(t.caretime) as caretime from  SALE_CustomerCare t  ");
			buffer.append("  where (sysdate-t.caretime  )>8  ");
			buffer.append("  group by  t.principal ) tt ");
			buffer.append("  where t.caretime=tt.caretime and  t.principal=tt.principal ");
			List list=domainProviderSetImpl.getDao().getBySql(buffer.toString(), CustomerCare.class);
			if(!list.isEmpty()){
				for (Object item : list) {
					CustomerCare bo=(CustomerCare)item;
					RemindForm form=buildRemindFormByCustomerCare(bo);
					remindService.editRemind(form);
				}
			}
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("发送客户关怀提醒失败!", e);
		}
	}
	
	private  RemindForm buildRemindFormByCustomerCare(CustomerCare  customerCare) throws BusinessException{
		//暂时不针对每个客户的销售机会进行检查
		//Customer customer =customerCare.getCustomer(domainProviderSet);
		String msg="您这个月没有对客户进行过关怀";
		RemindForm form=new RemindForm();
		form.setObjId(customerCare.getId().toString());
		form.setObjClassName("CustomerCare");
		form.setName(msg);
		form.setSendDate(new Date());
		form.setRemindUser(customerCare.getPrincipal());
		form.setHandleState(0);
		return form;
	}
	
	public IRemindService getRemindService() {
		return remindService;
	}

	public void setRemindService(IRemindService remindService) {
		this.remindService = remindService;
	}
}
